<?php

class Comment extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();
    
    function __construct()
    {
        parent::__construct();
        
        self::$home_data = array();
        $this->load->model('comment_model');
    }
	
    function index()
    {
        //
    }

    /**
     * Get comment 
     */
    public function getListComment()
    {
        //Get params
        $nodeID = $this->input->get('nodeID');
        $limit = $this->input->get('limit');
        $offset = $this->input->get('offset');

        //Get comment
        $data = $this->comment_model->getListCommentByNodeID($nodeID, $limit, $offset);

        //Set data
        self::$home_data['arrComment'] = $data['data'];
        self::$home_data['totalComment'] = $data['total'];

        //Assign to view
        $this->load->view('comment/comment_data', self::$home_data);
    }

    public function insertComment()
    {
        //Get params
        $nodeID = $this->input->post('nodeID');

        $name = $this->input->post('name');
        $name = str_replace('<', '&lt;', $name);

        $email = $this->input->post('email');
        $email = str_replace('<', '&lt;', $email);

        $content = $this->input->post('content');
        $content = str_replace('<', '&lt;', $content);
        $content = substr($content, 0, 500);

        //Set params
        $params = array(
            'node_id'        =>  $nodeID,
            'user_name'     =>  $name,
            'user_email'    =>  $email,
            'content'       =>  $content,
            'created_on'    =>  time()
        );

        //Insert data
        $commentID = $this->comment_model->insertComment($params);

        //return JSON data
        echo json_encode(array('commentID' => $commentID));
        exit();
    }

}
